/* @file SHJList.Java NZB file list display and updating
 * @author Jay Schulist jjschlst@gmail.com
 * 
 * Copyright (C) 2007 Jay Schulist
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package stickyhand.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * need prototype cells for READY_TO_START, DOWNLOADING, FINISHED
 * @author jjschlst
 *
 */
public class SHJList extends JList {
	static Color listForeground, listBackground,
		listSelectionForeground, listSelectionBackground;
	static {
		UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
		listForeground = uid.getColor("List.foreground");
		listBackground = uid.getColor("List.background");
		listSelectionForeground = uid.getColor("List.selectionForeground");
		listSelectionBackground = uid.getColor("List.selectionBackground");
	}
	
	ImageIcon fileIcon;
	
	JComponent fileCellPrototype;
	JLabel fileNameLabel, fileSizeLabel;
	
	public SHJList() {
		super();
		buildPrototypeCells();
		this.setCellRenderer(new SHListCellRenderer());
		this.setModel(new DefaultListModel());
	}
	
	private void buildIcons() {
		fileIcon = StickyHand.createImageIcon("images/fileImage.gif", "NZB file");
	}
	
	protected void buildPrototypeCells() {
		buildIcons();
		
		fileCellPrototype = new JPanel();
		fileCellPrototype.setLayout(new GridBagLayout());
		addWithGridBag(new JLabel(fileIcon), fileCellPrototype, 0, 0, 1, 2,
				GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 0);
		
		fileNameLabel = new JLabel();
		Font defaultLabelFont = fileNameLabel.getFont();
		Font nameFont = defaultLabelFont.deriveFont(Font.PLAIN, defaultLabelFont.getSize());
		fileNameLabel.setFont(nameFont);
		addWithGridBag(fileNameLabel, fileCellPrototype, 1, 0, 1, 1,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 1, 0);
		
		fileSizeLabel = new JLabel();
		Font defaultSizeLabelFont = fileSizeLabel.getFont();
		Font sizeFont = defaultSizeLabelFont.deriveFont(Font.PLAIN, defaultSizeLabelFont.getSize() - 4);
		fileSizeLabel.setFont(sizeFont);
		addWithGridBag(fileSizeLabel, fileCellPrototype, 1, 1, 1, 1,
				GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, 1, 0);
		
		opacify(fileCellPrototype);
	}
	
	private void addWithGridBag(Component comp, Container cont, int x, int y,
			int width, int height, int anchor, int fill, int weightx, int weighty) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.anchor = anchor;
		gbc.fill = fill;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		cont.add(comp, gbc);
	}
	
	private void opacify(Container prototype) {
		Component[] comps = prototype.getComponents();
		for (int i=0; i < comps.length; i++) {
			if (comps[i] instanceof JComponent)
				((JComponent)comps[i]).setOpaque(true);
		}
	}
	
	static public String formatBytes(int bytes) {
		final int kb = 1024;
		final int mb = kb * 1000;
		final int gb = mb * 1000;
		float sz = 0;
		
		StringBuilder result = new StringBuilder();
		if (bytes > gb) {
			sz = bytes / gb;
			result.append(sz + " GB");
		} else if (bytes > mb) {
			sz = bytes / mb;
			result.append(sz + " MB");
		} else if (bytes > kb) {
			sz = bytes / kb;
			result.append(sz + " KB");
		} else {
			result.append(bytes + " bytes");
		}
		return result.toString();
	}
	
	public class SHListCellRenderer extends JPanel implements ListCellRenderer {

	    public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {

	    	SHFileItem item = (SHFileItem)value;
	    	fileNameLabel.setText(item.nzb.getName());
	    	fileSizeLabel.setText(formatBytes(item.nzb.getBytes()));
	    	setColorsForSelectionState(fileCellPrototype, isSelected);
	    	return fileCellPrototype;
	    }
	    
	    private void setColorsForSelectionState(Container prototype, boolean isSelected) {
	    	Component[] comps = prototype.getComponents();
	    	for (int i=0; i< comps.length; i++) {
	    		if (isSelected) {
	    			comps[i].setForeground(listSelectionForeground);
	    			comps[i].setBackground(listSelectionBackground);
	    		} else {
	    			comps[i].setForeground(listForeground);
	    			comps[i].setBackground(listBackground);
	    		}
	    	}
	    }
	}
}
